<template>
  <div class="search">
    <i class="iconfont icon-sousuo"></i>
    <input
      type="text"
      @focus="showBox = true"
      placeholder="搜索"
      v-model="searchText"
    />
    <div class="searchBox" v-show="showBox">
      <p class="text" @click="goSearchPage">
        <a>{{ searchText }}</a>
        <a>相关的结果</a>
      </p>
      <div class="box">
        <h4>单曲</h4>
        <ul>
          <li v-for="(item, index) in songlist" :key="index" @click="goSong">
            {{ item.name }}-{{ item.artists[0].name }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      showBox: false,
      songlist: [],
    };
  },

  computed: {
    searchText: {
      get() {
        return this.$store.state.searchText;
      },
      async set(newval) {
        this.$store.commit("getIptVal", newval);
        //发送请求
        var res = await this.$http(`search?keywords=${newval}`);
        this.songlist = res.data.result.songs.slice(0, 6);
      },
    },
  },
  methods: {
    //去搜索结果页
    goSearchPage() {
      this.showBox = false;
      this.$router.push({
        path: "/findSongPage",
        query: {
          t: Date.now(),
        },
      });
    },
    goSong() {
      console.log(2);
    },
  },
};
</script>

<style lang="less" scoped>
.search {
  display: flex;
  width: 160px;
  height: 28px;
  background-color: #e13e3e;
  border-radius: 14px;
  margin-right: 12px;
  color: #f6cdcd;
  .icon-sousuo {
    margin: 8px 12px;
    font-size: 14px;
  }
  input {
    background-color: #e13e3e;
    border: 0;
    width: 100px;
    height: 26px;
    outline: none;
    color: #f6cdcd;
  }
  .searchBox {
    position: absolute;
    width: 360px;
    height: 200px;
    border-radius: 5px;
    background-color: #fff;
    top: 50px;
    left: 250px;
    font-size: 12px;
    border: 2px solid #f8f8f8;

    .text {
      margin-left: 30px;
      color: #717171;
      padding: 5px 0;
      cursor: pointer;
      & a:nth-child(1) {
        color: #6f94bd;
      }
    }
    .box {
      h4 {
        padding-left: 30px;
        background-color: #f5f5f7;
        color: #414142;
      }
      li {
        padding-left: 30px;
        margin: 9px 0;
        color: #333333;
        cursor: pointer;
      }
    }
  }
}
</style>